package com.mysterymall.Common.Comp;

import com.mysterymall.Common.Utils.BaseEnum;
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
 
public class OrderDelay implements Delayed{
	
	private final String orderId;
    private final Long createTime; // 创建时间
    
    public OrderDelay(String orderId,Long createTime){
        this.orderId = orderId;
        this.createTime =  createTime;
    }
 
    // 判断对象是否过期(小于等于0时过期)
    @Override
    public long getDelay(@NotNull TimeUnit unit) {
    	return createTime + Integer.parseInt(BaseEnum.Order_Expiration_Time.getCode())*60* 1000L - System.currentTimeMillis(); // 创建订单24小时未支付即过期
    }
    
    // 根据对象过期时间排序, 哪个元素最早到过期时间, 就排在前面 
	@Override
	public int compareTo(@NotNull Delayed delayed) {
		OrderDelay that = (OrderDelay)delayed;
        if(this.createTime>that.createTime){
            return 1;
        }else if(this.createTime.equals(that.createTime)){
            return 0;
        }else {
            return -1;
        }
	}
 
	public String getOrderId() {
		return orderId;
	}
 
	public Long getCreateTime() {
		return createTime;
	}
	
}